package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.SellerListActivity;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.FoodInfo;
import com.xgkj.diyiketang.util.ImageCache;

import java.util.ArrayList;
import java.util.List;

public class MyGridViewAdapter extends BaseAdapter {
	int count;
	Context context;
	private List<FoodInfo> mList = new ArrayList<FoodInfo>();
	private String areaId;

	public String getAreaId() {
		return areaId;
	}

	public void setAreaId(String areaId) {
		this.areaId = areaId;
	}

	public MyGridViewAdapter(Context context, int count) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.count = count;
	}

	public void updateData(List<FoodInfo> list) {
		if (mList != null) {
			mList.addAll(list);
		}
		notifyDataSetChanged();
	}

	public void clearData() {
		if (mList != null) {
			mList.clear();
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if(mList !=null){
			return mList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(mList != null){
			return mList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		// TODO Auto-generated method stud
		areaId = getAreaId();
		final ViewHolder holder;
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.listitem, null);
			holder.iv = (ImageView) convertView.findViewById(R.id.foodLogo);
			holder.tv = (TextView) convertView.findViewById(R.id.foodName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		//holder.tv.setText("num" + (position + 1) + " ");
		final FoodInfo finfo = mList.get(position);
		if(finfo!=null){
			String foodlogo = finfo.getfoodLogo();
			if(foodlogo != null && foodlogo.length()>0){
				ImageCache.getInstance(context).loadBitmapToView(holder.iv,foodlogo,null);

			}
			holder.iv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String fdid = finfo.getfoodId();
					Intent intent = new Intent(context, SellerListActivity.class);
					intent.putExtra("FOODID", fdid);
					intent.putExtra("AREAID", areaId);
					context.startActivity(intent);
				}
			});
//			holder.iv.setOnTouchListener(new View.OnTouchListener() {
//				@Override
//				public boolean onTouch(View v, MotionEvent event) {
//					if (event.getAction() == MotionEvent.ACTION_MOVE) {
//						parent.requestDisallowInterceptTouchEvent(false);
//						return false;
//					}else if(event.getAction() == MotionEvent.ACTION_DOWN){
//						parent.requestDisallowInterceptTouchEvent(true);
//						return true;
//					}
//					return false;
//				}
//			});

			holder.tv.setText(finfo.getfoodName());

		}
		return convertView;
	}

	class ViewHolder {
		ImageView iv;
		TextView tv;
	}

}